import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

import '../extension/index.dart';
import 'index.dart';

///时间格式化
class CommonDateFormat {
  static const String dashYmdHms = "yyyy-MM-dd HH:mm:ss";
  static const String dashYmdHm = "yyyy-MM-dd HH:mm";
  static const String dashYmd = "yyyy-MM-dd";
  static const String dashYm = "yyyy-MM";
  static const String dashMd = "MM-dd";

  static const String slantYmdHms = "yyyy/MM/dd HH:mm:ss";
  static const String slantYmdHm = "yyyy/MM/dd HH:mm";
  static const String slantYmd = "yyyy/MM/dd";
  static const String slantYm = "yyyy/MM";
  static const String slantMd = "MM/dd";

  static const String pointYmdHms = "yyyy.MM.dd HH:mm:ss";
  static const String pointYmdHm = "yyyy.MM.dd HH:mm";
  static const String pointYmd = "yyyy.MM.dd";
  static const String pointYm = "yyyy.MM";
  static const String pointMdHm = "MM.dd HH:mm";
  static const String pointMd = "MM.dd";

  static const String hms = "HH:mm:ss";
  static const String hm = "HH:mm";
  static const String yyyy = "yyyy";

  ///小于10的拼接0(处理成两位显示)
  static String _appendZero(int time) {
    return time < 10 ? "0$time" : "$time";
  }

  static String _getTime(DateTime start) {
    try {
      var cur = DateTime.now();
      //今年之前的 ymd
      if (cur.year - start.year > 0) {
        return "${start.year}.${_appendZero(start.month)}.${_appendZero(start.day)}";
      }
      //昨天之前的 md
      if (cur.month - start.month > 0 || cur.day - start.day > 1) {
        return "${_appendZero(start.month)}.${_appendZero(start.day)}";
      }
      //昨天的
      if (cur.day - start.day == 1) {
        return "昨天${_appendZero(start.hour)}:${_appendZero(start.minute)}";
      }
      //今天的
      if (cur.hour > start.hour || cur.minute - start.minute > 1) {
        return '${_appendZero(start.hour)}:${_appendZero(start.minute)}';
      }
      //一分钟内的
      return "刚刚";
    } catch (e) {
      Log.log("getNewsTime error : ${e.toString()}");
      return '';
    }
  }

  ///获取消息的时间
  static String getMessageTime(String time) {
    /// time -> 2022-05-19 22:11:10
    if (time.isEmpty) return time;
    return _getTime(DateTime.parse(time));
  }

  static String appendZero(int time) => _appendZero(time);

  ///时间格式转换
  static String switchTime({required String time, required String fromFormat, required String toFormat}) {
    return _format(time, fromFormat, toFormat);
  }

  ///yyyy-MM-dd HH:mm:ss -> yyyy.MM.dd HH:mm
  static String dashYmdHmsToDashYmd(String time) {
    return _format(time, dashYmdHms, dashYmd);
  }

  ///yyyy-MM-dd HH:mm:ss -> yyyy.MM.dd HH:mm
  static String dashYmdHmsToPointYmdHm(String time) {
    return _format(time, dashYmdHms, pointYmdHm);
  }

  ///yyyy-MM-dd HH:mm:ss -> yyyy.MM.dd
  static String dashYmdHmsToPointYmd(String time) {
    return _format(time, dashYmdHms, pointYmd);
  }

  ///yyyy-MM-dd HH:mm:ss -> MM.dd HH:mm
  static String dashYmdHmsToPointMdHm(String time) {
    return _format(time, dashYmdHms, pointMdHm);
  }

  ///yyyy-MM-dd HH:mm:ss -> HH:mm
  static String dashYmdHmsToHm(String time) {
    return _format(time, dashYmdHms, hm);
  }

  ///yyyy-MM-dd -> yyyy
  static String dashYmdToYYYY(String time) {
    return _format(time, dashYmd, yyyy);
  }

  ///yyyy-MM-dd -> yyyy.MM
  static String dashYmdToPointYm(String time) {
    return _format(time, dashYmd, pointYm);
  }

  ///yyyy-MM-dd -> yyyy.MM.dd
  static String dashYmdToPointYmd(String time) {
    return _format(time, dashYmd, pointYmd);
  }

  ///yyyy-MM -> yyyy
  static String dashYmToYYYY(String time) {
    return _format(time, dashYm, yyyy);
  }

  ///HH:mm:ss -> HH:mm
  static String dashHmsToHm(String time) {
    return _format(time, hms, hm);
  }

  static String getCreateTimeToPoint(String time) {
    return _format(time, dashYmdHms, pointYmd);
  }

  static String getYmdDashToPoint(String time) {
    return _format(time, dashYmd, pointYmd);
  }

  static String getCreateTimeNoSeconds(String time) {
    return _format(time, dashYmdHms, dashYmdHm);
  }

  static String getTimeFromTimeInterval(int? timeInterval, {String format = dashYmdHms}) {
    if (timeInterval == null || timeInterval.toString().length < 10) return '';
    DateTime date = DateTime.fromMillisecondsSinceEpoch(timeInterval);
    var f = DateFormat(format);
    return f.format(date);
  }

  static String _format(String time, String from, String to) {
    try {
      var f = DateFormat(from);
      var t = DateFormat(to);
      return t.format(f.parse(time));
    } catch (e) {
      Log.log("format : $e");
      return time;
    }
  }

  ///处理开始时间 结束时间的,结束时间不能小于开始时间
  static void reduceStartAndEndTime(
    ValueNotifier<String> start,
    ValueNotifier<String> end, {
    String? chooseStartTime,
    String? chooseEndTime,
  }) {
    String startTime = start.value;
    String endTime = end.value;
    Log.log('startTime = $startTime, endTime = $endTime ,'
        ' chooseStartTime = $chooseStartTime ,chooseEndTime = $chooseEndTime');
    if (chooseStartTime.notEmpty) {
      if (endTime.notEmpty && _compareTime(chooseStartTime!, endTime) > 0) {
        start.value = endTime;
        end.value = chooseStartTime;
      } else {
        start.value = chooseStartTime!;
      }
    } else if (chooseEndTime.notEmpty) {
      if (startTime.notEmpty && _compareTime(startTime, chooseEndTime!) > 0) {
        start.value = chooseEndTime;
        end.value = startTime;
      } else {
        end.value = chooseEndTime!;
      }
    }
  }

  ///比较时间，大于0 互换
  static int _compareTime(
    String a,
    String b, {
    String append = '-',
  }) {
    List<String> aTime = a.split(append);
    List<String> bTime = b.split(append);
    if (aTime.length == 1) {
      aTime.add('0');
    }
    if (bTime.length == 1) {
      bTime.add('0');
    }
    int year = (int.tryParse(aTime[0]) ?? 0) - (int.tryParse(bTime[0]) ?? 0);
    if (year != 0) return year;
    return (int.tryParse(aTime[1]) ?? 0) - (int.tryParse(bTime[1]) ?? 0);
  }
}
